Page({
  /**
    * 页面的初始数据
    */
  data: {
    volunteer:[],
    showName:false,
    showEmail:false,
    showTel:false,
    locateion: '',
    latitude:'',
    longitude:'',
    // 普通选择器数据
    array:['空闲', '忙碌'],
    index:0,
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var volunteerId_get = wx.getStorageSync('volunteerId');
    var url = 'https://www.lanotherl.com/volunteer/findVolunteer/' + volunteerId_get
    wx.request({
      url,
      success:res=>{
        console.log("查询当前志愿者的信息：",res)
        this.setData({
          volunteer:res.data.data.volunteer
        })
        console.log("获取到的志愿者信息为：",this.data.volunteer)
      }
    })
  },

  /**
    * 控制显示
    */
  // 邮箱输入框显示控制
  emailClick:function(){
    this.setData({
      showEmail:true
    })
  },
  telClick:function(){
    this.setData({
      showTel:true
    })
  },
  /**
  * 点击返回按钮隐藏
  */
  back:function(){
    this.setData({
      showName:false,
      showEmail:false,
      showTel:false
    })
  },
  /**
  * 获取input输入值
  */
  wish_put:function(e){
    this.setData({
      textV:e.detail.value
    })
  },
  /**
  * 点击确定按钮获取input值并且关闭弹窗
  */
  // 姓名确认
  name:function(){
    console.log(this.data.textV)
    this.setData({
      showName:false,
      name:this.data.textV
    })
    wx.showToast({
      title: '姓名修改成功',
    })
  },
  // 邮箱确认
  email:function(){
    console.log(this.data.textV)
    let str = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
    var volunteerId_get = wx.getStorageSync('volunteerId');
    if (str.test(this.data.textV)) {
     console.log("志愿者id为：",volunteerId_get) 
      wx.request({
        url: 'https://www.lanotherl.com/volunteer/updateVolunteerInfo',
        method:'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data:{
          volunteerId:volunteerId_get,
          email:this.data.textV
        },
        success:res=>{
          console.log("修改邮箱接口：",res);
          let email_lat = 'volunteer.email';
          this.setData({
            showEmail:false,
            [email_lat]:this.data.textV
          })
          wx.showToast({
            title: '邮箱修改成功',
          })
        }
      })
    } else {
      wx.showToast({
        image: '../../images/icon/jinggao.png' ,
        title: '请输入正确邮箱',
      })
    }
  },
  // 电话确认
  tel:function(){
    console.log(this.data.textV)  
    let str = /^1\d{10}$/
    var volunteerId_get = wx.getStorageSync('volunteerId');
    if (this.data.textV.length === 11&&str.test(this.data.textV)) {
      wx.request({
        url: 'https://www.lanotherl.com/volunteer/updateVolunteerInfo',
        method:'POST',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        data:{
          volunteerId:volunteerId_get,
          phoneNumber:this.data.textV
        },
        success:res=>{
          console.log("修改电话接口：",res);
          let phoneNumber_lat = 'volunteer.phoneNumber';
          this.setData({
            showTel:false,
            [phoneNumber_lat]:this.data.textV
          })
          wx.showToast({
            title: '电话修改成功',
          })
        }
      })
    } else {
      wx.showToast({
        title: '手机号不正确',
        image: '../../images/icon/jinggao.png'
      })
    }
  },
  /**
   * 选择当前地点
   */
  chooseNowAddress: function () {
    var volunteerId_get = wx.getStorageSync('volunteerId');
    var _page = this;
    wx.chooseLocation({ //选择当前地点，调用chooseLocation
      success: res => {
        _page.setData({
          locateion: res.name,
          latitude:res.latitude,
          longitude:res.longitude
        });
        console.log("区域地址：",_page.data.locateion);
        console.log("纬度：",_page.data.latitude);
        console.log("经度", _page.data.longitude);
        wx.request({
          url: 'https://www.lanotherl.com/volunteer/updateVolunteerInfo',
          method:'POST',
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          data:{
            volunteerId:volunteerId_get,
            address:_page.data.locateion
          },
          success:(res)=>{
            console.log("修改所属区域接口：",res);
            let address_lat = 'volunteer.phoneNumber';
            this.setData({
              [address_lat]:_page.data.locateion
            })
            wx.showToast({
              title: '所属区域修改成功',
            })
            wx.request({
              url: 'https://www.lanotherl.com/volunteer/pos',
              method:'PUT',
              header: {
                "content-type": "application/x-www-form-urlencoded"
              },
              data:{
                volunteerId:volunteerId_get,
                latitude:_page.data.latitude,
                longitude:_page.data.longitude
              },
              success:res=>{
                console.log("更新经纬度接口：",res);
              }
            })
          }
        })
      },
      fail: e => {
        console.log(e);
        // 判断用户是否拒绝了授权
        wx.getSetting({
          success: res => {
            if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
              // 用户拒绝了授权
              wx.showModal({
                title: '提示',
                content: '您拒绝了定位权限，将无法使用该功能',
                success: res => {
                  if (res.confirm) {
                    // 跳转设置页面
                    wx.openSetting({
                      success: res => {
                        if (res.authSetting['scope.userLocation']) {
                          // 授权成功，重新定位
                          wx.getLocation({
                            success: res => {}
                          });
                        } else {
                          // 没有允许定位权限
                          wx.showToast({
                            title: '您拒绝了定位权限，将无法使用该功能',
                            icon: 'none'
                          })
                        }
                      }
                    });
                  }
                }
              });
            }
          }
        });
      }
    });
  },
  /**
    * 任务下拉框，注意状态的数组下标要加1后再上传到服务器中
    */
   changeValue(e) {
    console.log("更新空闲状态：",e)
    this.setData({
      index:e.detail.value
    })
  },
})